{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/12_zonal_statistics.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_youtube(\"ou-Xm3CLitM\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Add Earth Engine dataset\n",
    "dem = ee.Image(\"USGS/SRTMGL1_003\")\n",
    "\n",
    "# Set visualization parameters.\n",
    "dem_vis = {\n",
    "    \"min\": 0,\n",
    "    \"max\": 4000,\n",
    "    \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n",
    "}\n",
    "\n",
    "# Add Earth Engine DEM to map\n",
    "Map.addLayer(dem, dem_vis, \"SRTM DEM\")\n",
    "\n",
    "# Add Landsat data to map\n",
    "landsat = ee.Image(\"LANDSAT/LE7_TOA_5YEAR/1999_2003\")\n",
    "\n",
    "landsat_vis = {\"bands\": [\"B4\", \"B3\", \"B2\"], \"gamma\": 1.4}\n",
    "Map.addLayer(landsat, landsat_vis, \"LE7_TOA_5YEAR/1999_2003\")\n",
    "\n",
    "states = ee.FeatureCollection(\"TIGER/2018/States\")\n",
    "Map.addLayer(states, {}, \"US States\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_dir = os.path.join(os.path.expanduser(\"~\"), \"Downloads\")\n",
    "out_dem_stats = os.path.join(out_dir, \"dem_stats.csv\")\n",
    "\n",
    "if not os.path.exists(out_dir):\n",
    "    os.makedirs(out_dir)\n",
    "\n",
    "# Allowed output formats: csv, shp, json, kml, kmz\n",
    "# Allowed statistics type: MEAN, MAXIMUM, MINIMUM, MEDIAN, STD, MIN_MAX, VARIANCE, SUM\n",
    "geemap.zonal_stats(dem, states, out_dem_stats, stat_type=\"MEAN\", scale=1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_landsat_stats = os.path.join(out_dir, \"landsat_stats.csv\")\n",
    "geemap.zonal_stats(landsat, states, out_landsat_stats, stat_type=\"SUM\", scale=1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.create_download_link(out_dem_stats)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.create_download_link(out_landsat_stats)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
